﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel.DataTransfer;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Documents;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using 纸片人.win2dEffectControl;
using 纸片人.win2dEffectControl.DataClass;

// The Templated Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234235

namespace 纸片人.CalculateControls
{
    public  class Vector2Control : RangeControl
    {

        #region 属性

        /// <summary>
        /// 获取或设置OneGenre的值
        /// </summary>  
        public Object OneGenre
        {
            get { return (Object)GetValue(OneGenreProperty); }
            set { SetValue(OneGenreProperty, value); }
        }

        /// <summary>
        /// 标识 OneGenre 依赖属性。
        /// </summary>
        public static readonly DependencyProperty OneGenreProperty =
            DependencyProperty.Register("OneGenre", typeof(Object), typeof(Vector2Control), new PropertyMetadata("X：", OnOneGenreChanged));

        private static void OnOneGenreChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            Vector2Control target = obj as Vector2Control;
            Object oldValue = (Object)args.OldValue;
            Object newValue = (Object)args.NewValue;
            if (oldValue != newValue)
                target.OnOneGenreChanged(oldValue, newValue);
        }

        protected virtual void OnOneGenreChanged(Object oldValue, Object newValue)
        {

        }

        /// <summary>
        /// 获取或设置TwoGenre的值
        /// </summary>  
        public Object TwoGenre
        {
            get { return (Object)GetValue(TwoGenreProperty); }
            set { SetValue(TwoGenreProperty, value); }
        }

        /// <summary>
        /// 标识 TwoGenre 依赖属性。
        /// </summary>
        public static readonly DependencyProperty TwoGenreProperty =
            DependencyProperty.Register("TwoGenre", typeof(Object), typeof(Vector2Control), new PropertyMetadata("Y：", OnTwoGenreChanged));

        private static void OnTwoGenreChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            Vector2Control target = obj as Vector2Control;
            Object oldValue = (Object)args.OldValue;
            Object newValue = (Object)args.NewValue;
            if (oldValue != newValue)
                target.OnTwoGenreChanged(oldValue, newValue);
        }

        protected virtual void OnTwoGenreChanged(Object oldValue, Object newValue)
        {
        }



        #region Vector2     
        /// <summary>
        /// 获取或设置Vector2的值
        /// </summary>  
        public Vector2 Vector2
        {
            get { return (Vector2)GetValue(Vector2Property); }
            set { SetValue(Vector2Property, value); }
        }

        /// <summary>
        /// 标识 Vector2 依赖属性。
        /// </summary>
        public static readonly DependencyProperty Vector2Property =
            DependencyProperty.Register("Vector2", typeof(Vector2), typeof(Vector2Control), new PropertyMetadata(new Vector2(), OnVector2Changed));

        private static void OnVector2Changed(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            Vector2Control target = obj as Vector2Control;
            Vector2 oldValue = (Vector2)args.OldValue;
            Vector2 newValue = (Vector2)args.NewValue;
            if (oldValue != newValue)
                target.OnVector2Changed(oldValue, newValue);
        }

        protected virtual void OnVector2Changed(Vector2 oldValue, Vector2 newValue)
        {
            
        }
        #endregion


        #endregion

        protected TextBoxSlider oneSlider;
        protected TextBoxSlider twoSlider;

        public Vector2Control()
        {
            this.DefaultStyleKey = typeof(Vector2Control);
           
        }

        protected override void OnLoaded()
        {
            base.OnLoaded();
            if (oneSlider != null)
            {
                oneSlider.Value = Vector2.X;
            }
            if (twoSlider != null)
            {
                twoSlider.Value = Vector2.X;
            }
            oneSlider.Header = null;
            twoSlider.Header = null;
        }

        

        protected override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            oneSlider = GetTemplateChildFormName<TextBoxSlider>(nameof(oneSlider));
            twoSlider = GetTemplateChildFormName<TextBoxSlider>(nameof(twoSlider));

        }



        protected override void Timer_Tick(object sender, object e)
        {
            if (oneConnectElement != null)
            {

                var exporter = oneConnectElement.Exporter;

                try
                {
                    var value = GetProvideValue(exporter, exproterValueName);
                    if(value is Vector2 v2)
                    {
                        if (v2.X != Vector2.X || v2.Y != Vector2.Y)
                        {
                            Vector2 = v2;
                        }
                        if (oneSlider != null && oneSlider.Value != Vector2.X)
                        {
                            oneSlider.Value = Vector2.X;
                        }
                        if (twoSlider != null && twoSlider.Value != Vector2.Y)
                        {
                            twoSlider.Value = Vector2.Y;
                        }
                    }
                }
                catch
                {
                    exporter.Disconnect(oneConnectElement);
                    exporter = null;
                }

            }
            else
            {

                if (oneSlider.Value != Vector2.X || twoSlider.Value != Vector2.Y)
                {
                    Vector2 = new Vector2((float)oneSlider.Value, (float)twoSlider.Value);
                }
            }
        }

        protected override void OnexporterDrag(UIElement sender, DragStartingEventArgs args)
        {
            OnDragExportInvoke();
            OutputData od = new OutputData();
            od.Type = OutputType.Vector2;
            od.Exporter = this;
            od.ExportConnectElement = (ExporterDragControl)sender;
            od.ExporterInfo = nameof(Vector2);
            EffectOutputControl.outputData = od;
        }

        protected override void OnProviderEnter(object sednder, DragEventArgs e)
        {
            if (EffectOutputControl.outputData != null && EffectOutputControl.outputData.Type == OutputType.Vector2)
            {
                e.AcceptedOperation = DataPackageOperation.Copy;
                e.DragUIOverride.Caption = "拖进来了";
            }
        }

        protected override void OnProviderDrop(object sender, DragEventArgs e)
        {
            var outputData = EffectOutputControl.outputData;
            //判断是否形成了闭环了
            if (IsChartLoop(outputData.Exporter))
                return;
            if (outputData != null && outputData.Type == OutputType.Vector2)
            {
                var exporter = EffectOutputControl.outputData.Exporter;
                if (outputData.ExporterInfo is string str)
                {
                    exproterValueName = str;
                }
                else
                {
                    return;
                }
                (providerDrop as ProviderDropControl)?.SetConnectState();
                CreateConnect(ref oneConnectElement, providerDrop);
            }
        }
    }
}
